package com.molihub.controller;

import com.alibaba.fastjson.JSONArray;
import com.molihub.entity.RequestMessage;
import com.molihub.entity.User;
import com.molihub.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName: RequestMessageController
 * @Description: 处理有关请求消息的控制器
 * @author: GGBOY
 * @date 2020/2/2 16:05
 * @Version: 1.0
 **/
@Controller
public class RequestMessageController extends BaseController {

    private static Logger logger = LogUtils.getInstance(RequestMessageController.class);

    /**
     * 根据用户名D获得未接受的好友请求消息
     *
     * @param username 用户名
     * @return java.lang.String
     * @author GGBOY
     * @date 2020/2/2
     */
    @ResponseBody
    @GetMapping("/request/message/byReceive/{username}")
    public String getRequestMessageByReceiveUserId(@PathVariable String username) {
        User user = userService.getUserByUserName(username);
        // 获得登录用户的所有未接受的好友请求消息
        List<RequestMessage> messages = findMessageAboutReceiveUser(user.getUserId());
        JSONArray jsonArray = new JSONArray();
        if (messages != null && messages.size() > 0) {
            for (RequestMessage message :
                    messages) {
                logger.info("遍历messages:--> " + message.toString());
                jsonArray.add(message.getMessageContent());
            }
        }
        return jsonArray.toJSONString();
    }

    /**
     * 查找有关用户的未接受的好友请求消息
     *
     * @param bUserId 用户ID
     * @return com.molihub.entity.RequestMessage
     * @author GGBOY
     * @date 2020/2/2
     */
    private List<RequestMessage> findMessageAboutReceiveUser(Integer bUserId) {
        return requestMessageService.findMessageByReceiveUserId(bUserId);
    }


    /**
     * 将通知状态设为已读
     *
     * @param bUserName 接收通知的用户名
     * @param aUserName 发通知的用户名
     * @return java.lang.String
     * @author GGBOY
     * @date 2020/2/2
     */
    @ResponseBody
    @GetMapping("/request/message/read/notice/{aUserName}/{bUserName}/{flag}")
    public String readNotice(@PathVariable String bUserName, @PathVariable String aUserName, @PathVariable Integer flag) {
        // 获取用户双方各自的ID
        User aUser = userService.getUserByUserName(aUserName);
        User bUser = userService.getUserByUserName(bUserName);
        // 更新对应通知的状态，即将is_receive设置为1，即接收状态
        List<RequestMessage> messageByIdAndFlag = requestMessageService.findMessageByIdAndFlag(aUser.getUserId(), bUser.getUserId(), flag);
        logger.info("readNotice............ " + messageByIdAndFlag.size() + messageByIdAndFlag.toString());
        for (RequestMessage requestMessage : messageByIdAndFlag) {
            requestMessage.setIsReceive(1);
            // 更新该通知的状态
            requestMessageService.updateMessage(requestMessage);
        }
        return "OK";
    }

    /**
     * 将全部通知设为已读
     *
     * @param userName 用户名
     * @return java.lang.String
     * @author GGBOY
     * @date 2020/2/23
     */
    @ResponseBody
    @PostMapping("/request/message/readAll/notice/{userName}")
    public String readAllNotices(@PathVariable String userName) {
        User user = userService.getUserByUserName(userName);
        // 将request_message表中的数据设为已读
        requestMessageService.readAll(user.getUserId());
        // 将message表中的全部数据设为已读
        messageService.setMessagesLooked(user.getUserId(), 0);
        return "OK";
    }
}
